//
//  CartTableViewCell.m
//  MeterMart
//
//  Created by ENERGY on 2018/9/28.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "CartTableViewCell.h"

@interface CartTableViewCell ()
@property (nonatomic, strong)UIImageView * iconImg;
@property (nonatomic, strong)UILabel * titleLab;
@property (nonatomic, strong)UILabel * moneyLab;
@property (nonatomic, strong)UILabel * numLab;
@property (nonatomic, strong)UIButton * deleteBtn;
@property (nonatomic, strong)UIButton * totalMoneyBtn;
@property (nonatomic, strong)UIView * whiteView;
@property (nonatomic, strong)NSString * cartID;//购物车ID
@property (nonatomic, strong)NSString * goodssum;//商品数量
@end

@implementation CartTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = getColor(mainColor);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        __weak typeof (self)weakSelf = self;
        [self.contentView addSubview:self.whiteView];
        [self.contentView addSubview:self.iconImg];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.moneyLab];
        [self.contentView addSubview:self.numLab];
        [self.contentView addSubview:self.deleteBtn];
        [self.contentView addSubview:self.totalMoneyBtn];
        [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(10/WIDTH_6_SCALE);
            make.left.right.bottom.equalTo(weakSelf.contentView);
        }];
        [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.whiteView.mas_left).mas_offset(21/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.whiteView.mas_top).mas_offset(20/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.whiteView.mas_bottom).mas_offset(-20/WIDTH_6_SCALE);
            make.width.mas_equalTo(111/WIDTH_6_SCALE);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.iconImg.mas_right).mas_offset(28/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.whiteView.mas_top).mas_offset(26/WIDTH_6_SCALE);
            make.height.mas_lessThanOrEqualTo(31.5/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.whiteView.mas_right).mas_offset(15/WIDTH_6_SCALE);
        }];
        [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.iconImg.mas_right).mas_offset(43/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.whiteView.mas_top).mas_offset(75/WIDTH_6_SCALE);
        }];
        [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.whiteView.mas_right).mas_offset(-18/WIDTH_6_SCALE);
            make.centerY.top.equalTo(weakSelf.moneyLab);
        }];
        [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.iconImg.mas_bottom);
            make.right.equalTo(weakSelf.totalMoneyBtn.mas_left).mas_offset(-21/WIDTH_6_SCALE);
            make.width.mas_offset(52.5/WIDTH_6_SCALE);
            make.height.mas_offset(25/WIDTH_6_SCALE);
        }];
        [self.totalMoneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.whiteView.mas_right).mas_offset(-15/WIDTH_6_SCALE);
            make.centerY.top.width.equalTo(weakSelf.deleteBtn);
        }];
    }return self;
}
- (UIView *)whiteView{
    if (!_whiteView) {
        _whiteView = [[UIView alloc] init];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }return _whiteView;
}
- (UIImageView *)iconImg{
    if (!_iconImg) {
        _iconImg = [[UIImageView alloc] init];
        _iconImg.image = [UIImage imageNamed:@"YimiPlaceHolder"];
    }return _iconImg;
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = DEF_FontSize_14;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.lineBreakMode = 0;
        _titleLab.numberOfLines = 0;
        _titleLab.text = @"商品名";
    }return _titleLab;
}
- (UILabel *)moneyLab{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.font = DEF_FontSize_15;
        _moneyLab.textColor = [UIColor redColor];
        _moneyLab.textAlignment = NSTextAlignmentLeft;
        _moneyLab.text = @"￥8450";
    }return _moneyLab;
}
- (UILabel *)numLab{
    if (!_numLab) {
        _numLab = [[UILabel alloc] init];
        _numLab.font = DEF_FontSize_15;
        _numLab.textColor = [UIColor blackColor];
        _numLab.textAlignment = NSTextAlignmentRight;
        _numLab.text = @"x 1";
    }return _numLab;
}
- (UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [[UIButton alloc] init];
        _deleteBtn.titleLabel.font = DEF_FontSize_10;
        [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteBtn setBackgroundColor:getColor(mainColor)];
        [_deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _deleteBtn.layer.cornerRadius = 6;
        _deleteBtn.layer.masksToBounds = YES;
        [_deleteBtn setTitleColor:getColor(@"898989") forState:UIControlStateNormal];
    }return _deleteBtn;
}
- (UIButton *)totalMoneyBtn{
    if (!_totalMoneyBtn) {
        _totalMoneyBtn = [[UIButton alloc] init];
        _totalMoneyBtn.titleLabel.font = DEF_FontSize_10;
        [_totalMoneyBtn setTitle:@"结算" forState:UIControlStateNormal];
        [_totalMoneyBtn setBackgroundColor:[UIColor redColor]];
        [_totalMoneyBtn addTarget:self action:@selector(totalMoneyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _totalMoneyBtn.layer.cornerRadius = 6;
        _totalMoneyBtn.layer.masksToBounds = YES;
        [_totalMoneyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }return _totalMoneyBtn;
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    [self.iconImg sd_setImageWithURL:[NSURL URLWithString:model[@"goodsimg"]] placeholderImage:[UIImage imageNamed:@"YimiPlaceHolder"]];
    self.titleLab.text = model[@"goodsname"];
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@", model[@"groupprice"]];
    self.numLab.text = [NSString stringWithFormat:@"x %@", model[@"goodssum"]];
    self.goodssum = model[@"goodssum"];
    self.cartID = model[@"shoppingcarid"];
}

#pragma mark - action
- (void)deleteBtnClick:(UIButton *)sender{
    NSLog(@"删除");
    if (self.deleteProduceBlock) {
        self.deleteProduceBlock(self.cartID);
    }
}
- (void)totalMoneyBtnClick:(UIButton *)sender{
    NSLog(@"结算");
    if (self.listDetailBlock) {
        self.listDetailBlock(self.cartID, self.goodssum);
    }
}
@end
